#ifndef LinkBuffer_hpp
#define LinkBuffer_hpp

#include <stdio.h>
#include <iostream>

namespace HHLink {

    struct LinkBuffer {
//    std::shared_ptr<uint8_t[]> data;
        unsigned char *data;
        int length;
        unsigned char ID;
        unsigned char cmd;
        int cmdCode;
        int repeat; // 重复次数
//     int remainRepeat; // 剩余次数
        int32_t repeatInterval; // 重复间隔(毫秒)
        bool isResponse; // 是否有回复
        int timeout_ms; // 超时时间
        int retry_times; // 失败重试次数
    };


    void LinkBufferCreate(LinkBuffer *buffer, unsigned char *data, int length);
    void LinkBufferDestroy(LinkBuffer *buffer);
    void LinkBufferSetRepeat();


    class LinkResult {
    protected:
        unsigned char ID_;
        unsigned char cmd_;

    public:
        virtual bool result() const = 0;
        virtual ~LinkResult() = default;
        LinkResult(unsigned char cmd): cmd_(cmd) {ID_ = 0;}
        LinkResult(unsigned char ID, unsigned char cmd): cmd_(cmd), ID_(ID) {}
        unsigned char getCmd() {
            return cmd_;
        }
    };

    class LinkErrResult: public LinkResult {
    public:
        bool result() const override {
            return false;
        }
    };

    class LinkBooleanResult: public LinkResult {

    public:
        bool success;
        LinkBooleanResult(unsigned char cmd, unsigned char ID = 0): LinkResult(ID,cmd) {}
        bool result() const override {
            return success;
        }
    };


    class LinkStringResult: public LinkResult {
        char *payload_;
    public:
        bool result() const override {
            return payload_ != nullptr;
        };
        LinkStringResult(unsigned char cmd = 0): LinkResult(cmd) {}

        void setPayload(char *payload) {
            if (payload_) {
                delete payload_;
            }
            payload_ = static_cast<char*>(malloc(strlen(payload)));
            strcpy(payload_, payload);
        }
        char *getPayload() {
            return payload_;
        }
        ~LinkStringResult() {
            if (payload_) {
                delete payload_;
            }
        }
    };


}


#endif /* LinkBuffer_hpp */
